package dk.druidPro.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dk.druidPro.dao.RoleDaoI;
import dk.druidPro.dao.UserDaoI;
import dk.druidPro.model.Tuser;
import dk.druidPro.pageModel.PageUtil;
import dk.druidPro.pageModel.SessionInfo;
import dk.druidPro.pageModel.User;
import dk.druidPro.service.UserServiceI;
import dk.druidPro.util.MD5Util;

@Service
public class UserServiceImpl implements UserServiceI {

	@Autowired
	private UserDaoI userDao;
	
	@Autowired
	private RoleDaoI roleDao;


	public User login(User user) {
		Tuser t=new Tuser();
		BeanUtils.copyProperties(user, t);
		t.setPwd(MD5Util.md5(user.getPwd()));
		t=userDao.login(t);
		if(t==null){
			return null;
		}else{
			User loginUser=new User();
			BeanUtils.copyProperties(t, loginUser);
			return loginUser;
		}
	}


	synchronized public void add(User user) throws Exception {
		Tuser t=new Tuser();
		BeanUtils.copyProperties(user, t);
		if (userDao.queryCount(t)>0) {
			throw new Exception("登录名已存在！");
		} else {
			userDao.insert(t);
		}
	}


	public PageUtil dataGrid(User user, PageUtil ph) {
		Tuser u = new Tuser();
		BeanUtils.copyProperties(user, u);
		return userDao.queryPageList(u, ph);
	}


	public void delete(String id) {
		userDao.delete(id);
	}


	synchronized public void edit(User user) throws Exception {
		Tuser t=new Tuser();
		BeanUtils.copyProperties(user, t);
		userDao.update(t);
	}


	public boolean editCurrentUserPwd(SessionInfo sessionInfo, String oldPwd,
			String pwd) {
		return false;
	}



	public User get(String id) {
		
		Tuser t=userDao.getById(id);
		User user=new User();
		if(t!=null){
			BeanUtils.copyProperties(t, user);
		}
		return user;
		
	}


	public void grant(String ids, User user) {
//		if (ids != null && ids.length() > 0) {
//			List<Trole> roles = new ArrayList<Trole>();
//			if (user.getRoleIds() != null) {
//				for (String roleId : user.getRoleIds().split(",")) {
//					roles.add(roleDao.get(Trole.class, roleId));
//				}
//			}
//			for (String id : ids.split(",")) {
//				if (id != null && !id.equalsIgnoreCase("")) {
//					Tuser t = userDao.get(Tuser.class, id);
//					t.setTroles(new HashSet<Trole>(roles));
//				}
//			}
//		}
	}


	public List<User> loginCombobox(String q) {
		return null;
	}


	public void reg(User user) throws Exception {
		
	}


	public List<String> resourceList(String id) {
		List<String> resourceList = new ArrayList<String>();
//		Map<String, Object> params = new HashMap<String, Object>();
//		params.put("id", id);
//		Tuser t = userDao.get("from Tuser t join fetch t.troles role join fetch role.tresources resource where t.id = :id", params);
//		if (t != null) {
//			Set<Trole> roles = t.getTroles();
//			if (roles != null && !roles.isEmpty()) {
//				for (Trole role : roles) {
//					Set<Tresource> resources = role.getTresources();
//					if (resources != null && !resources.isEmpty()) {
//						for (Tresource resource : resources) {
//							if (resource != null && resource.getUrl() != null) {
//								resourceList.add(resource.getUrl());
//							}
//						}
//					}
//				}
//			}
//		}
		return resourceList;
	}


	public List<Long> userCreateDatetimeChart() {
		return null;
	}

	public PageUtil loginCombogrid(String q, PageUtil ph) {
		return null;
	}


	public void editPwd(User user) {
		// TODO Auto-generated method stub
		
	}
}
